function crea() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else {
        return new ActiveXObject('Microsoft.XMLHTTP');
    }
}
var xhr;
function user(us){
        var reg1 = /^1[3578]\d{9}$/;
        var reg2 = /^\w{2,8}@[0-9a-zA-Z]{3,6}(.com)$/;
        if(reg1.test(us.value) || reg2.test(us.value)){
            us.nextElementSibling.innerHTML = '√';
            us.nextElementSibling.style.color = 'green';
            return true;
        }else{
            us.nextElementSibling.innerHTML = '请输入正确的用户'
            us.nextElementSibling.style.color = 'red';
            return false;
        }
}
function pwd(pas){
    var flag
    var reg3 = /^([a-z]|\d|[A-Z]){8,10}$/;
    if(reg3.test(pas.value)){
        pas.nextElementSibling.innerHTML = '√';
        pas.nextElementSibling.style.color = 'green';
        flag = true;
    }else{
        pas.nextElementSibling.innerHTML = '请输入正确的密码'
        pas.nextElementSibling.style.color = 'red';
        flag = false;
    }
    var repass = document.getElementById('repass');
    if(repass.value){
        repeatPwd(repass);
    }
    return flag;
}
function repeatPwd(rep){
    var pass= document.getElementById('pass');
    if(rep.value == ''){
        rep.nextElementSibling.innerHTML = '密码不能空';
        rep.nextElementSibling.style.color = 'red';
        return false;
    }else if(pass.value == rep.value){
        rep.nextElementSibling.innerHTML = '√';
        rep.nextElementSibling.style.color = 'green';
        return true;
    }else{
        rep.nextElementSibling.innerHTML = '与上面密码不一致'
        rep.nextElementSibling.style.color = 'red';
        return false;
    }
}
function sub(){
    var form =document.getElementById('for');
 
    var userFlag = user(form.acc);
    var pwdFlag = pwd(form.pass);
    var repFlag = repeatPwd(form.repass);
    if(userFlag && pwdFlag && repFlag){
            xhr = crea();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    // console.log(xhr.responseText);
                    // var regist = JSON.parse(xhr.responseText);
                    // console.log(regist);
                 if(xhr.responseText ==='注册成功'){
                    window.location = "http://127.0.0.1:5500/html/head.html";
                 }else{
                     alert('注册失败,请重新注册');
                 }
                }
            }
        }
        xhr.open('post', 'http://192.168.10.102:8888/regsterUser');
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send("username=" + form.acc.value + "&password=" + form.pass.value);
    }
    // alert('注册成功，请返回登录');
}